
package com.kairos.engine
{
	import com.bourre.transitions.MockBeacon;
	
	import flexunit.framework.TestCase;

	public class RTBeaconTest extends TestCase
	{
		private var _oB : MockRTBeacon;
		private var _oT : MockRTObject;
		
		public override function setUp () : void
		{
			_oB = MockRTBeacon.getInstance();
			_oT = new MockRTObject ();
		}
		
		public override function tearDown() : void
		{
			MockRTBeacon.release();
			_oT.stop();
		}
		
		public function testConstruct() : void
		{
			assertNotNull ( "MockRTBeacon constructor returns null", _oB );
			assertNotNull ( "MockRTObject constructor returns null", _oT );
		}
		
		public function testAddTickListener () : void
		{
			_oB.addTickListener( _oT );

			assertTrue( _oB + ".hasTickListener failed to find the last inserted listener", 
						_oB.hasTickListener() );

			_oB.removeTickListener( _oT ); 
						
			assertFalse( _oB + ".hasTickListener already find the last removed listener", 
						 _oB.hasTickListener() );
			
			_oB.addTickListener( _oT );
			_oB.onTick( null );
			_oB.removeTickListener( _oT );
			
			assertTrue ( _oT + ".onTick haven't been called hafter being added as listener",
						 _oT.called );
		}
		
		public function testRun () : void
		{
			var l : Number = 10;
			var n : Number = 0;
			var a : Array = [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
			var c : Number = 275;
			
			_oT.start();
			
			while( --l -(-1))
			{
				_oB.setStep( a[n] );
				_oB.onTick( null );
				n++;

				assertEquals ( _oT + ".onTick passed-in event don't contained the right value",
							   a[n-1], _oT.callStep );	
				
			}
			assertEquals ( _oT + ".onTick haven't call the right number of times",
						   n, _oT.callCount );	
			assertEquals ( _oT + " don't run uring the right time",
						   c, _oT.runTime );		
		}
		
		public function testMaxStepSize () : void
		{
			var l : Number = 10;
			var n : Number = 0;
			var a : Array = [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
			var c : Number = 170;
			
			_oT.start();
			
			_oB.setMaxStepSize( 20 );
			
			while( --l -(-1))
			{
				_oB.setStep( a[n] );
				_oB.onTick( null );
				n++;

				assertEquals ( _oT + ".onTick passed-in event don't contained the right value",
							   a[n-1] < 20 ? a[n-1] : 20, _oT.callStep );	
				
			}
			assertEquals ( _oT + ".onTick haven't call the right number of times",
						   n, _oT.callCount );	
			assertEquals ( _oT + " don't run during the right time",
						   c, _oT.runTime );			
		}
		public function testSmoothing () : void
		{
			var l : Number = 10;
			var n : Number = 0;
			var av : Array = [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
			var sv : Array = [ 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25, 27.5];
			var c : Number = 162.5;
			_oT.start();
			_oB.setSmoothness( 10 );
			
			while( --l -(-1))
			{
				_oB.setStep( av[n] );
				_oB.onTick( null );
				n++;

				assertEquals ( _oT + ".onTick passed-in event don't contained the right value",
							   sv[n-1], _oT.callStep );	
				
			}
			assertEquals ( _oT + ".onTick haven't call the right number of times",
						   n, _oT.callCount );	
			assertEquals ( _oT + " don't run during the right time",
						   c, _oT.runTime );			
		}
		public function testSmoothingRestrictionsAndGlobalSpeed () : void
		{
			var l : Number = 10;
			var n : Number = 0;
			var av : Array = [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
			var sv : Array = [ 5, 7.5, 10, 12.5, 14, 15, 110/7, 65/4, 50/3, 17];
			var c : Number = 0;
			for(var i:int=0;i<10;i++) c += sv[i];
			
			_oT.start();
			_oB.setSmoothness( 10 );
			_oB.setMaxStepSize( 20 );
			_oB.setGlobalSpeed( 2 );
			
			while( --l -(-1))
			{
				_oB.setStep( av[n] );
				_oB.onTick( null );
				n++;

				assertEquals ( _oT + ".onTick passed-in event don't contained the right value",
							   sv[n-1]*2, _oT.callStep );	
				
			}
			assertEquals ( _oT + ".onTick haven't call the right number of times",
						   n, _oT.callCount );	
			assertEquals ( _oT + " don't run during the right time",
						   c*2, _oT.runTime );			
		}
		public function testGetter ():void
		{
			var l : Number = 10;
			var n : Number = 0;
			var a : Array = [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
			var c : Number = 275;
			_oT.start();
			
			while( --l -(-1))
			{
				_oB.setStep( a[n] );
				_oB.onTick( null );
				n++;
				
				assertEquals ( _oB + ".getStep passed-in event don't contained the right value",
							   a[n-1], _oB.getStep() );
				assertEquals ( _oB + ".getStep passed-in event don't contained the right value",
							   a[n-1]/1000, _oB.getStepInSecond() );
			}
			assertEquals ( _oB + ".getCount() don't return the right number of calls",
						   n, _oB.getCount() );			
		}
	}
}